# https://leetcode-cn.com/problems/happy-number/


class Solution:
    def isHappy(self, n: int) -> bool:
        n_set = set()
        while n != 1:
            n = self.generate(n)
            if n in n_set:
                return False
            n_set.add(n)
        return True

    def generate(self, n: int) -> int:
        ret = 0
        while n != 0:
            ret += (n%10) ** 2
            n = n//10
        return ret

s = Solution()
print(s.isHappy(19))
